﻿using System;
using System.Collections.Generic;
using System.Text;

namespace lxwmvc
{
    using System;
    using System.Xml;
    using System.Web;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Data;
    using Newtonsoft.Json;
 
    /// <summary>
    /// veloctiy 前台帮助类
    /// </summary>
    public class funhelper
    {
        /// <summary>
        /// 加法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static object jiafa(object a,object b)
        {
            return (a.todecimal() + b.todecimal());
        }


        /// <summary>
        /// 减法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static object jianfa(object a, object b)
        {
            return (a.todecimal() - b.todecimal());
        }

        /// <summary>
        /// 乘法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static object chengfa(object a, object b)
        {
            return (a.todecimal() * b.todecimal());
        }

        /// <summary>
        /// 判断表达式
        /// </summary>
        /// <param name="a"></param>
        /// <param name="c"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static bool panduan(object a, object c, object b)
        {
            string biaodanshi = c.tostr();
            if (biaodanshi == ">=")
                return a.todecimal(0) >= b.todecimal(0);
            else if (biaodanshi == ">")
                return a.todecimal(0) > b.todecimal(0);
            else if (biaodanshi == "=")
                return a.todecimal(0) == b.todecimal(0);
            else if (biaodanshi == "<")
                return a.todecimal(0) < b.todecimal(0);
            else if (biaodanshi == "<=")
                return a.todecimal(0) <= b.todecimal(0);
            else if (biaodanshi == "!=")
                return a.todecimal(0) != b.todecimal(0);

            return false;
        }

        /// <summary>
        /// 除法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static object chufa(object a, object b)
        {
            return (a.todecimal() / b.todecimal());
        }

        /// <summary>
        /// 转换decimal
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static object todecimal(object a)
        {
            return a.todecimal(0);
        }

        /// <summary>
        /// 转换成整数
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static object toint(object a)
        {
            return a.toint32(0);
        }

        /// <summary>
        /// 余数
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static object yushu(object a, object b)
        {
            return (a.toint32() % b.toint32());
        }

        

        ///// <summary>
        ///// 输出json对象
        ///// </summary>
        ///// <param name="count"></param>
        ///// <param name="dt"></param>
        ///// <returns></returns>
        //public static string json(int count, DataTable dt)
        //{
        //    Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
        //    timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

        //    string result = "";
        //    var json = new { total = count, rows = dt };
        //    result = JsonConvert.SerializeObject(json, Newtonsoft.Json.Formatting.Indented, timeConverter);
        //    return result;
        //}

        /// <summary>
        /// 得到session的内容
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object session(string key)
        {
            return HttpContext.Current.Session[key];
        }

        /// <summary>
        /// 得到cookies的内容
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object cookie(string key)
        {
            return cookieshelper.getcookie(key);
        }



        //public static object test(params object[] args)
        //{
        //    return "safdsfasdf";
        //}

        //public static object test1(string bb,string arg="aaa")
        //{
        //    return arg;
        //}

        /// <summary>
        /// 分割字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="splitstr"></param>
        /// <returns></returns>
        public static string[] split(object obj, string splitstr = ",")
        {
            return stringhelper.split(obj.tostr(), splitstr);
        }

        /// <summary>
        /// #foreach($item in $funhelper.array("a","b","c","d"))
        /// $item.key $item.value
        /// #end
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static List<object> array(params object[] args)
        {
            if (args == null || args.Length == 0) return null;
            List<object> lst = new List<object>();
            for (int i = 0; i < args.Length / 2; i++)
            {
                lst.Add(new { key=args[i*2],value=args[i*2+1] });
            }

            return lst;
        }

        /// <summary>
        /// 强大的switch函数 
        /// </summary>
        /// <param name="v"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static object encode(object v, params object[] args)
        {
            if (args == null) return "";
            bool b = args.Length % 2 == 0;
            
            //if (b)
            {
                for (int i = 0; i < args.Length / 2; i++)
                {
                    if (args[i*2].tostr() == v.tostr()) return args[i*2 + 1];
                }
            }


            if (!b) return args[args.Length - 1];

            return "";
        }        
    }
}
